package il.ac.bgu.cs.heuristics.graph.pddl.parser.types;

import il.ac.bgu.cs.heuristics.utils.CollectionUtils;

import java.util.Collections;
import java.util.List;

/**
 * An immutable representation of a logical expression in a pddl
 * 
 * @author Ronen Pinko
 * 
 */
public final class LogicalExpression{ // NOPMD builder pattern
	private final LogicalOperator operator;
	private final List<Predicate> predicates;

	private LogicalExpression(LogicalExpression.Builder builder) {
		this.predicates = builder.predicates;
		this.operator = builder.logicalOperator;
	}

	public final static class Builder extends ExpressionBuilder{

		private final List<Predicate> predicates = CollectionUtils
		        .newArrayList();

		public LogicalExpression build() {
			if (predicates.isEmpty()) {
				throw new IllegalArgumentException(
				        "logical expression must contain at least one predicate");
			}
			return new LogicalExpression(this); // NOPMD builder pattern
		}

		public LogicalExpression.Builder addPredicate(Predicate predicate) {
			this.predicates.add(predicate);
			return this;
		}

	}

	public LogicalOperator getOperator() {
		return operator;
	}

	public List<Predicate> getPredicates() {
		return Collections.unmodifiableList(predicates);
	}

	@Override
	public String toString() {
		return "Expression [(" + operator + " " + predicates + ")]";
	}

}